package dk.aiszone.lang.sql;

import dk.aiszone.lang.sql.interfaces.SQLConstraint;

/**
 * A list of comma seperated elements.
 * 
 * @author Ais
 */
/* pp */class SQLValueList extends SQLList {

    /**
     * Constructor for the <code>SQLComma</code> class.
     * 
     * @param part1
     *            The first part in the <code>SQLComma</code> list.
     */
    /* pp */SQLValueList(SQLAssignableField part1) {
        super(new HolderClass(part1));
    }

    @Override
    protected String getSeperator() {
        return ", ";
    }

    private static class HolderClass implements SQLConstraint {

        private final SQLAssignableField field;

        /* pp */HolderClass(SQLAssignableField field) {
            this.field = field;
        }

        @Override
        public SQLAssignableField getAssignableField() {
            return field;
        }

        @Override
        public SQLResult prepareSQL(SQLResult sql) {
            return sql.append(toSQL());
        }

        @Override
        public String toPreparedStatement() {
            return "?";
        }

        @Override
        public String toSQL() {
            Object value = field.getValue();
            if (value == null) {
                return "null";
            } else if (String.class.equals(value.getClass())) {
                return "'" + (String)value + "'";
            } else if (Boolean.class.equals(value.getClass()) || Number.class.isAssignableFrom(value.getClass())) {
                return value.toString();
            }

            throw new IllegalArgumentException("Unknown value class: " + value.getClass());
        }
    }
}